
/**
 * 时间格式取年月日
 * @param time
 * @returns {*}
 */
function getYearMonthDate(time) {
    if(time!=null) {
        var YMD = time.split(" ")[0];  // 年月日
        var HMS = time.split(" ")[1];  // 时分秒
        return YMD;
    }
    return null;
}

layui.config({
    base: '../../static/js/layuiModulesEX/'
});
layui.use(['paging','form'], function() {
    var $ = layui.jquery,
        paging = layui.paging(),
        layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
        layer = layui.layer, //获取当前窗口的layer对象
        laytpl = layui.laytpl,
        form = layui.form();

    //获取全部岗位信息
    axios.get('/PositionType/findBatchBySchoolId/'+teacher.schoolid)
        .then(function (response) {
            // console.log(response.data);
            for (var i = 0; i < response.data.data.length; i++) {
                positionTypes[response.data.data[i].id] = response.data.data[i].typename;
            }
            console.log(positionTypes);
            //有效岗位类别
            var validPositionTypes = response.data.data;
            var html = '';
            for (var i in validPositionTypes) {
                html += '<option value=' + validPositionTypes[i].id + '>' + validPositionTypes[i].typename + '</option>';
            }
            $('select[name=positiontype]').append(html);
            form.render('select');
        })
        .catch(function (response) {
            console.log(response);
        });
    // console.log(teacher);
    paging.init({
        openWait: true,
        url:'/recruitInfo/findByCondition',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
            isTemporary:1,
            schoolid:teacher.schoolid,
            publisherid:''
        },
        contentType : "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function(msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function() { //完成的回调
            //alert('处理完成');
            //绑定所有编辑按钮事件
            $('#content').children('tr').each(function() {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var id = $that.children('a[data-opt=detail]').data('id');
                // console.log(id);
                /**
                 * 编辑按钮
                 */
                $that.children('a[data-opt=more]').on('click', function() {
                    axios.get('/recruitInfo/find/'+id)
                        .then(function (response) {
                            console.log(response.data);
                            vm.recruitinfo = response.data.data;
                            vm.recruitinfo.state = 2;
                            console.log(vm.recruitinfo);
                            $('#showWorkingPic').attr('src','');
                            $('#Vue input[type=file]').show();
                            $('select[name=type]').next().find('[lay-value="' + vm.recruitinfo.type + '"]').click();
                            $('select[name=positiontype]').next().find('[lay-value="' + vm.recruitinfo.positiontype + '"]').click();
                            $('#Vue input[value="提交"]').show();
                            $('#Vue input[value="暂存"]').show();
                            if (vm.recruitinfo.workingpic != null&&vm.recruitinfo.workingpic!=0) {
                                axios.get('/image/find/'+vm.recruitinfo.workingpic)
                                    .then(function (res) {
                                        // console.log(res.data.data);
                                        $('#showWorkingPic').attr('src', res.data.data);
                                    })
                                    .catch(function (msg) {
                                        console.log(msg);
                                    })
                            }
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                    vm.show = false;
                });
                /**
                 * 删除按钮
                 */
                $that.children('a[data-opt=delete]').on('click', function() {
                    layer.confirm('确认删除？',function (index) {
                        axios.get('/recruitInfo/remove/'+id)
                            .then(function (response) {
                                console.log(response);
                            })
                            .catch(function (response) {
                                console.log(response);
                            });
                        layer.close(index);
                        location.reload();

                    });

                });

                /**
                 * 详情按钮
                 */
                $that.children('a[data-opt=detail]').on('click',function () {
                    console.log(id);
                    axios.get('/recruitInfo/find/'+id)
                        .then(function (response) {
                            // console.log(response.data);
                            vm.recruitinfo = response.data.data;
                            $('#showWorkingPic').attr('src','');
                            $('#Vue input[type=file]').hide();
                            $('select[name=type]').next().find('[lay-value="' + vm.recruitinfo.type + '"]').click();
                            $('select[name=positiontype]').next().find('[lay-value="' + vm.recruitinfo.positiontype + '"]').click();
                            $('#Vue input[value="提交"]').hide();
                            $('#Vue input[value="暂存"]').hide();
                            if (vm.recruitinfo.workingpic != null&&vm.recruitinfo.workingpic!=0) {
                                axios.get('/image/find/'+vm.recruitinfo.workingpic)
                                    .then(function (res) {
                                        // console.log(res.data.data);
                                        $('#showWorkingPic').attr('src', res.data.data);
                                    })
                                    .catch(function (msg) {
                                        console.log(msg);
                                    })
                            }
                            vm.show = false;
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                })
            });

        }
    });

    //搜索
    $('#search').on('click', function() {
        paging.get({
            title:$('#searchVal').val(),
            publisherid:publisherid
        });
        parent.layer.alert('你点击了搜索按钮')
    });

    var vm = new Vue({
        el:'#Vue',
        data:{
            show:true,
            recruitinfo:{
                id:''
            }
        },
        methods:{
            /**
             * 添加按钮
             */
            add:function () {
                vm.show=false;
                //重置数据并清除脏数据 默认为提交状态
                vm.recruitinfo={state:2,publisherid:publisherid};
            },
            /**
             * 暂时保存
             */
            temporarilySaved:function () {
                vm.recruitinfo.state = 1;
                vm.saveOrUpdate();
            },
            /**
             * 查询按钮
             */
            search:function () {
                paging.get({
                    title:$('#searchVal').val()
                });
            },
            /**
             * 插入或更新数据库记录
             */
            saveOrUpdate:function () {
                var url = vm.recruitinfo.id == null ? "/recruitInfo/add" : "/recruitInfo/modify";
                axios.post(url,vm.recruitinfo)
                    .then(function (response) {
                        layer.msg(response.data.message,{time:1500},function () {
                            if (response.data.success){
                                vm.reload();
                            }
                        });

                    })
                    .catch(function (response) {
                        console.log(response);
                    })
            },
            reload:function () {
                vm.show=true;
                paging.get();
            },
            myRecruit:function () {
                paging.get({isTemporary:-1, schoolid:'', publisherid:publisherid})
            },
            allRecruit:function () {
                paging.get({isTemporary:1, schoolid:teacher.schoolid, publisherid:''})
            }
        }
    });

});